import io
import time
from PIL import Image

from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC

from selenium import webdriver  # 引入模块,conda env list查看IDE配置的python路径，在script下装驱动
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait

from CanvasSeleniumAPI import *

'''
安装路径https://zhuanlan.zhihu.com/p/655420296
'''
chrome_options = Options()
chrome_options.page_load_strategy = 'eager'
web_driver = webdriver.Chrome(options=chrome_options)
# driver = webdriver.Chrome()
web_driver.maximize_window()  # 最大化窗口
try:
    web_driver.get("chrome://dino/")
except WebDriverException as e:
    if "net::ERR_INTERNET_DISCONNECTED" in str(e):
        print("Internet connection seems to be down!")
    else:
        print("An unexpected WebDriver exception occurred:", e)
runner_canvas = web_driver.find_element(By.CLASS_NAME, "runner-canvas")


# 获取Canvas的长和宽
s_width, s_height = get_width_height_of_canvas(runner_canvas)

runner_container = web_driver.find_element(By.CLASS_NAME, "runner-container")

runner_canvas.click()
runner_container.send_keys(Keys.SPACE)
collision = False
time.sleep(5)  # hjh设定的事件。

get_size_of_game(web_driver)  # 获取游戏页面的大小，便于后续参数的调整。
# pixels = get_pixels_value(web_driver, runner_canvas, 0, 0, s_width, s_height)  # 获取Canvas的像素值
# save_canvas_to_png_file(pixels, s_width, s_height)  # 将画布的图像保存到一个png文件
start_y = 100  # Dino头顶最上边的Y位置为94
end_y = 120   # 水平线的Y位置在131
start_x = 90  # Dino嘴巴最右边的X位置为66
end_x = 130    # 与start_x一起定义检测区域的宽度

while not collision:
    # 碰撞检测
    # start_time = time.time()
    # pixels = get_pixels_value(web_driver, runner_canvas, 0, 0, s_width, s_height)  # 获取Canvas的像素值，方案1
    # pixels = get_pixels_value(web_driver, runner_canvas, 0, 90, s_width, 40)  # 获取Canvas的像素值，方案2
    pixels = get_pixels_value(web_driver, runner_canvas, start_x, start_y, end_x-start_x, end_y-start_y)  # 获取Canvas的像素值
    # end_time = time.time()
    # duration_in_ms = (end_time - start_time) * 1000
    # print(f"获取像素值运行时间：{duration_in_ms:.4f}毫秒")
    # 碰撞检测的事件非常短，通常不到1毫秒。
    # is_ready_collision = collision_detection(pixels, s_width, 0)  # 障碍迫近，js有个铃声提示，找到这个变量应该会很快 ，方案1
    # is_ready_collision = collision_detection(pixels, s_width, 90)  # 调试通过，有时候能过上百个障碍。，方案2
    is_ready_collision = optimize_collision_detection(pixels, end_x-start_x, end_y-start_y)  # 优化版本。
    # print('is_ready_collision = ', is_ready_collision)
    if is_ready_collision:
        runner_container.send_keys(Keys.SPACE)
        # time.sleep(0.1)
    # collision = web_driver.execute_script('return Runner().crashed;')  # 太麻烦太慢了，js脚本快很多
    # print("collision = " + str(collision))
    # time.sleep(0.01)  # 不考虑软件的效率，则不休息。此处可用分支语句，添加关于效率的代码。

web_driver.close()
web_driver.quit()
print("Task is over.")
